package com.zhkj.txg.module.home.ui

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.tbruyelle.rxpermissions2.RxPermissions
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.home.adapter.ShopListAdapter
import com.zhkj.txg.module.home.vm.ShopListViewModel
import com.zhkj.txg.utils.LocationUtils
import kotlinx.android.synthetic.main.activity_shop_list.*


class ShopListActivity : ViewModelActivity<ShopListViewModel>() {

    private val shopListAdapter = ShopListAdapter()

    override fun setViewModel():
            ShopListViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_shop_list

    private var isPermission = false

    override fun initUI() {
        super.initUI()
        with(rvShopList) {
            layoutManager = LinearLayoutManager(this@ShopListActivity, LinearLayoutManager.VERTICAL, false)
            adapter = shopListAdapter
        }

        tvShopListIn.setOnClickListener {
            val shopInfo = shopListAdapter.getShopInfo()
            if (shopInfo == null) {
                toast("请选择门店")
                return@setOnClickListener
            }
            val gson = Gson()
            val data = Intent()
            data.putExtra(AccountManager.SHOP_INFO, gson.toJson(shopInfo))
            setResult(Activity.RESULT_OK, data)
            finish()
        }
    }

    @SuppressLint("CheckResult")
    override fun initData() {
        super.initData()


        val rxPermission = RxPermissions(this)
        rxPermission.request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
            .subscribe { permission ->
                isPermission = permission
                if (!permission) {
                    toast("请到设置中开启位置权限")
                } else {
                    val location = LocationUtils.getInstance().getLocations(this)
                    val lng = location.substring(location.indexOf(",") + 1,location.length)
                    val lat = location.substring(0,location.indexOf(","))
                    val map = hashMapOf("lng" to lng, "lat" to lat)
                    viewModel.getShopList(map).takeIf {
                        !it.hasObservers()
                    }?.observe(this, Observer {
                        handlerResponseStatus(it.status, it.statusTip)
                        if (it.status == HttpResponseStatus.Status.SUCCESS) {
                            val data = it.response?.data
                            val gson = Gson()
                            if (data != null) {
                                shopListAdapter.addData(data)
                            }
                        } else if (it.status == HttpResponseStatus.Status.FAILURE) {

                        }
                    })
                }
            }

    }
}